package com.example.houtai.util;


import com.example.houtai.SMS.ResultModel;
import com.example.houtai.SMS.http.dto.request.SmsSingleRequest;
import com.example.houtai.SMS.http.dto.response.SmsResponse;
import com.example.houtai.SMS.util.AES;
import com.example.houtai.SMS.util.GZIPUtils;
import com.example.houtai.SMS.util.JsonHelper;
import com.example.houtai.SMS.util.http.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 郝政博
 * SMSUtile
 */
public class SMSUtil {

    /**
     * @param appId     应用ID
     * @param secretKey 密钥
     * @param host      接口地址
     * @param algorithm 加密算法
     * @param encode    拓展码
     * @param isGizp    是否压缩
     * @param code      验证码
     * @param minute    创建时间
     * @param mobile    电话号码
     */
    public static void smsSend(String appId, String secretKey, String host, String algorithm, String encode, boolean isGizp, String code, String minute, String mobile) {
        setSingleSms(appId,
                secretKey,
                host,
                algorithm,
                "【维数公寓】您的短信验证码为 " + code + "，" + minute + " 分钟内有效，请勿告知他人。",
                null,
                null,
                mobile, isGizp, encode);
    }


    /**
     * 发送单条短信
     *
     * @param isGzip 是否压缩
     */
    public static void setSingleSms(String appId, String secretKey, String host, String algorithm, String content, String customSmsId, String extendCode, String mobile, boolean isGzip, String encode) {
        System.out.println("=============begin setSingleSms==================");
        ///发送单条短息服务的实体类
        SmsSingleRequest pamars = new SmsSingleRequest();
        pamars.setContent(content);// 短信内容
        pamars.setCustomSmsId(customSmsId);
        pamars.setExtendedCode(extendCode);
        pamars.setMobile(mobile);//手机号
        ResultModel result = request(appId, secretKey, algorithm, pamars, host + "/inter/sendSingleSMS", isGzip, encode);
        System.out.println("result code :" + result.getCode());
        if ("SUCCESS".equals(result.getCode())) {
            SmsResponse response = JsonHelper.fromJson(SmsResponse.class, result.getResult());
            if (response != null) {
                System.out.println("data : " + response.getMobile() + "," + response.getSmsId() + "," + response.getCustomSmsId());
            }
        }
        System.out.println("=============end setSingleSms==================");
    }


    /**
     * 公共请求方法
     */
    public static ResultModel request(String appId, String secretKey, String algorithm, Object content, String url, final boolean isGzip, String encode) {
        Map<String, String> headers = new HashMap<String, String>();
        HttpRequest<byte[]> request = null;
        try {
            headers.put("appId", appId);
            headers.put("encode", encode);
            String requestJson = JsonHelper.toJsonString(content);
            System.out.println("result json: " + requestJson);
            byte[] bytes = requestJson.getBytes(encode);
            System.out.println("request data size : " + bytes.length);
            if (isGzip) {
                headers.put("gzip", "on");
                bytes = GZIPUtils.compress(bytes);
                System.out.println("request data size [com]: " + bytes.length);
            }
            byte[] parambytes = AES.encrypt(bytes, secretKey.getBytes(), algorithm);
            System.out.println("request data size [en] : " + parambytes.length);
            HttpRequestParams<byte[]> params = new HttpRequestParams<byte[]>();
            params.setCharSet("UTF-8");
            params.setMethod("POST");
            params.setHeaders(headers);
            params.setParams(parambytes);
            params.setUrl(url);
            if (url.startsWith("https://")) {
                request = new HttpsRequestBytes(params, null);
            } else {
                request = new HttpRequestBytes(params);
            }
        } catch (Exception e) {
            System.out.println("加密异常");
            e.printStackTrace();
        }
        HttpClient client = new HttpClient();
        String code = null;
        String result = null;
        try {
            HttpResponseBytes res = client.service(request, new HttpResponseBytesPraser());
            if (res == null) {
                System.out.println("请求接口异常");
                return new ResultModel(code, result);
            }
            if (res.getResultCode().equals(HttpResultCode.SUCCESS)) {
                if (res.getHttpCode() == 200) {
                    code = res.getHeaders().get("result");
                    if (code.equals("SUCCESS")) {
                        byte[] data = res.getResult();
                        System.out.println("response data size [en and com] : " + data.length);
                        data = AES.decrypt(data, secretKey.getBytes(), algorithm);
                        if (isGzip) {
                            data = GZIPUtils.decompress(data);
                        }
                        System.out.println("response data size : " + data.length);
                        result = new String(data, encode);
                        System.out.println("response json: " + result);
                    }
                } else {
                    System.out.println("请求接口异常,请求码:" + res.getHttpCode());
                }
            } else {
                System.out.println("请求接口网络异常:" + res.getResultCode().getCode());
            }
        } catch (Exception e) {
            System.out.println("解析失败");
            e.printStackTrace();
        }
        ResultModel re = new ResultModel(code, result);
        return re;
    }

}
